package com.beagle.njddapp.ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationManagerCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.beagle.njddapp.R;
import com.beagle.njddapp.base.DirverApiService;
import com.beagle.njddapp.base.NjOwnerApiService;
import com.beagle.njddapp.base.OilApiService;
import com.beagle.njddapp.base.RetrofitHttp;
import com.beagle.njddapp.bean.HandleTick;
import com.beagle.njddapp.bean.MessageBean;
import com.beagle.njddapp.bean.StringBean;
import com.beagle.njddapp.bean.dirver.OilOrderState;
import com.beagle.njddapp.bean.oil.OilBean;
import com.beagle.njddapp.bean.oil.OilHomeBean;
import com.beagle.njddapp.bean.owner.DIrverOrderBean;
import com.beagle.njddapp.core.LocationUploadService;
import com.beagle.njddapp.core.UserService;
import com.beagle.njddapp.event.GiveBackEvent;
import com.beagle.njddapp.event.OilChangeEvent;
import com.beagle.njddapp.net.BaseOilHttpListResponse;
import com.beagle.njddapp.net.BaseOilHttpResponse;
import com.beagle.njddapp.net.HttpResponseObserver;
import com.beagle.njddapp.net.RxSchedulers;
import com.beagle.njddapp.ui.activity.dirver.DirverOrderActivity;
import com.beagle.njddapp.ui.activity.oil.OilDeviceManagerActivity;
import com.beagle.njddapp.ui.activity.oil.OilOwnerOrderActivity;
import com.beagle.njddapp.ui.activity.owner.DeviceManagerActivity;
import com.beagle.njddapp.ui.activity.owner.FindDirverActivity;
import com.beagle.njddapp.ui.activity.owner.MessageActivity;
import com.beagle.njddapp.ui.activity.owner.OrderActivity;
import com.beagle.njddapp.ui.fragment.CommonMapfragment;
import com.beagle.njddapp.ui.fragment.DirverHomeFragment;
import com.beagle.njddapp.ui.fragment.Mapfragment;
import com.beagle.njddapp.core.LocationManager;
import com.beagle.njddapp.ui.fragment.OilHomeFragment;
import com.beagle.njddapp.utils.GpsUtil;
import com.beagle.njddapp.utils.StatusBarUtil;
import com.beagle.njddapp.weight.SwitchButton;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;
import java.util.LinkedHashMap;

import cn.jpush.android.api.JPushInterface;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.RequestBody;

import static com.beagle.njddapp.core.UserService.ROLE_DIRVER;
import static com.beagle.njddapp.core.UserService.ROLE_OIL;
import static com.beagle.njddapp.core.UserService.ROLE_OWNER;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, SwitchButton.OnCheckedChangeListener, AMapLocationListener {

    private TextView tvAddress;
    private RelativeLayout container;
    private Fragment fragment;
    private SwitchButton switchButton;
    private TextView tvLocation;
    private static final int TYPE_MAP = 1;//普通地图
    private static final int TYPE_OIL = 2;//加油车驾驶员首页
    private static final int TYPE_DIRVER = 3;//农机驾驶员首页
    private static final int TYPE_DIRVER_ORDER = 4;//农机驾驶员订单页
    private static final int TYPE_MAP_2 = 5;//导航地图
    private int current_type;
    private ImageView ivMessage;
    private ImageView ivMessageDirver;
    private TextView tvSwitch;
    private TextView tvMessage;
    private double msnLat, msLon;
    private LinearLayout llBottom;
    private OilOrderState oilOrderState;
    int fromtype;
    private Intent intent;
    private AMapLocationClient mlocationClient;
    private AMapLocationClientOption mLocationOption;
    private DIrverOrderBean dIrverOrderBean;
    private TextView tv1, tv2, tv3, tv4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intent = new Intent(this, LocationUploadService.class);
        StatusBarUtil.darkMode(this);
        EventBus.getDefault().register(this);
        findViewById(R.id.iv_back).setOnClickListener(this);
        tvAddress = findViewById(R.id.tv_address);
        ImageView ivMy = findViewById(R.id.iv_home);
        ivMy.setOnClickListener(this);
        ivMessage = findViewById(R.id.iv_message);
        tvMessage = findViewById(R.id.tv_meeeage);
        tvLocation = findViewById(R.id.tv_location);
        ivMessageDirver = findViewById(R.id.iv_message_dirver);
        ivMessage.setOnClickListener(this);
        ivMessageDirver.setOnClickListener(this);
        tvSwitch = findViewById(R.id.switch_tv);
        llBottom = findViewById(R.id.bottom_rl);
        fromtype = getIntent().getIntExtra("type", -1);
        container = findViewById(R.id.home_container);
        StatusBarUtil.setPaddingSmart(this, container);
        StatusBarUtil.immersive(this);
        switchButton = findViewById(R.id.switch_btn);
        switchButton.setOnCheckedChangeListener(this);
        tvLocation.setOnClickListener(this);
        tv1 = findViewById(R.id.tv1);
        tv2 = findViewById(R.id.tv2);
        tv3 = findViewById(R.id.tv3);
        tv4 = findViewById(R.id.tv4);
        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);
        tv4.setOnClickListener(this);
        getNotify();
        startLocation();
        if (UserService.getInstance().getRole() == ROLE_DIRVER) {
            Drawable drawable = getResources().getDrawable(R.mipmap.home_ic_work);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            tv1.setCompoundDrawables(null, drawable, null, null);
            tv1.setText("订单管理");
            tv2.setVisibility(View.INVISIBLE);
            tv3.setVisibility(View.INVISIBLE);
            tv4.setVisibility(View.INVISIBLE);
            switchButton.setVisibility(View.GONE);
            ivMessage.setVisibility(View.GONE);
            tvSwitch.setVisibility(View.GONE);
        } else if (UserService.getInstance().getRole() == ROLE_OIL) {
            Drawable drawable = getResources().getDrawable(R.mipmap.home_ic_oilcar);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            tv1.setCompoundDrawables(null, drawable, null, null);
            tv1.setText("加油车管理");
            tv2.setText("订单管理");
            Drawable drawable2 = getResources().getDrawable(R.mipmap.home_ic_work);
            drawable2.setBounds(0, 0, drawable2.getMinimumWidth(), drawable2.getMinimumHeight());
            tv2.setCompoundDrawables(null, drawable2, null, null);
            tv3.setVisibility(View.INVISIBLE);
            tv4.setVisibility(View.INVISIBLE);
        }
        if (UserService.getInstance().getRole() == ROLE_OIL) {
            ivMessage.setVisibility(View.GONE);
        }
        if (UserService.getInstance().getRole() == ROLE_OWNER) {
            switchButton.setVisibility(View.GONE);
            tvSwitch.setVisibility(View.GONE);
        }
        tvAddress.setText(LocationManager.getInstance().getCity());
        if (UserService.getInstance().getUserInfoBean() != null) {
            JPushInterface.setAlias(getApplicationContext(), 1, "prod." + UserService.getInstance().getUserInfoBean().getUserId());
        }
        message();
        tvAddress.postDelayed(new Runnable() {
            @Override
            public void run() {
                init();
            }
        }, 1000);

    }

    public void init() {
        if (UserService.getInstance().getRole() == ROLE_DIRVER) {
            getList();
        } else if (UserService.getInstance().getRole() == ROLE_OIL) {
            myCar();
        } else {
            addFragment(TYPE_MAP);
        }
        if (UserService.getInstance().getRole() == ROLE_OIL) {
            ivMessage.setVisibility(View.GONE);
        }
        if (UserService.getInstance().getRole() == ROLE_OWNER) {
            switchButton.setVisibility(View.GONE);
            tvSwitch.setVisibility(View.GONE);
        }
        tvAddress.setText(LocationManager.getInstance().getCity());
    }


    public void querySwitch() {
        retrofit2.Retrofit retrofit = RetrofitHttp.provideRetrofit();
        OilApiService apiService = retrofit.create(OilApiService.class);
        apiService.querySwitch().compose(RxSchedulers.compose()).safeSubscribe(new Observer<StringBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(StringBean switchBean) {
                if (switchBean != null) {
                    if ("open".equals(switchBean.getResult())) {
                        switchButton.setChecked(true);
                        addFragment(TYPE_OIL);
                    }else {
                        addFragment(TYPE_MAP);
                    }
                } else {
                    addFragment(TYPE_MAP);
                }
            }

            @Override
            public void onError(Throwable e) {
                addFragment(TYPE_MAP);
            }

            @Override
            public void onComplete() {

            }

        });
    }

    private void startLocation() {
        if (mlocationClient == null) {
            mlocationClient = new AMapLocationClient(this);
            mLocationOption = new AMapLocationClientOption();
            mLocationOption.setNeedAddress(true);//逆地理编码
            //设置定位监听
            mlocationClient.setLocationListener(this);
            //设置为高精度定位模式
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            //设置定位参数
            mlocationClient.setLocationOption(mLocationOption);
            // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
            // 注意设置合适的定位时间的间隔（最小间隔支持为2000ms），并且在合适时间调用stopLocation()方法来取消定位请求
            // 在定位结束后，在合适的生命周期调用onDestroy()方法
            // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
            mlocationClient.startLocation();
        }
    }

    private void getNotify() {
        NotificationManagerCompat notification = NotificationManagerCompat.from(this);
        boolean isEnabled = notification.areNotificationsEnabled();
        if (!isEnabled) {
            //未打开通知
            AlertDialog alertDialog = new AlertDialog.Builder(this)
                    .setTitle("注意")
                    .setMessage("为了更好的服务您，请在设置置中开启允许通知功能")
                    .setNegativeButton("否", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    })
                    .setPositiveButton("是", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                            Intent intent = new Intent();
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                                intent.putExtra("android.provider.extra.APP_PACKAGE", MainActivity.this.getPackageName());
                            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {  //5.0
                                intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                                intent.putExtra("app_package", MainActivity.this.getPackageName());
                                intent.putExtra("app_uid", MainActivity.this.getApplicationInfo().uid);
                                startActivity(intent);
                            } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {  //4.4
                                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                intent.addCategory(Intent.CATEGORY_DEFAULT);
                                intent.setData(Uri.parse("package:" + MainActivity.this.getPackageName()));
                            } else if (Build.VERSION.SDK_INT >= 15) {
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                                intent.setData(Uri.fromParts("package", MainActivity.this.getPackageName(), null));
                            }
                            startActivity(intent);

                        }
                    })
                    .create();
            alertDialog.show();
            alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(Color.BLACK);
            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.BLACK);
        }
    }


    public void message() {
        if (UserService.getInstance().getRole() == ROLE_OWNER) {
            retrofit2.Retrofit retrofit = RetrofitHttp.provideRetrofit();
            //获取接口对象
            NjOwnerApiService apiService = retrofit.create(NjOwnerApiService.class);
            RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), "{}");
            apiService.message(10 + "", 1 + "", body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpListResponse<MessageBean>>() {
                @Override
                public void success(BaseOilHttpListResponse<MessageBean> bean) {
                    if (bean != null && bean.results != null) {
                        if (bean.unReadCount > 0) {
                            tvMessage.setText(bean.unReadCount + "");
                            tvMessage.setVisibility(View.VISIBLE);
                        } else {
                            tvMessage.setVisibility(View.GONE);
                        }
                    } else {
                        tvMessage.setVisibility(View.GONE);

                    }
                }

                @Override
                public void error(int code, String message) {
                    tvMessage.setVisibility(View.GONE);

                }
            });
        }

    }


    public void getList() {
        retrofit2.Retrofit retrofit = RetrofitHttp.provideRetrofit();
        NjOwnerApiService njOwnerApiService = retrofit.create(NjOwnerApiService.class);
        Gson gson = new Gson();
        HashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("orderId", "");
        paramsMap.put("orderNum", "");
        paramsMap.put("orderStatus", "");
        String strEntity = gson.toJson(paramsMap);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        paramsMap.clear();
        njOwnerApiService.queryFindDriverOrderList(1000 + "", 1 + "", body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpListResponse<DIrverOrderBean>>() {
            @Override
            public void success(BaseOilHttpListResponse<DIrverOrderBean> orderBeanBaseOilHttpListResponse) {
                if (orderBeanBaseOilHttpListResponse != null && orderBeanBaseOilHttpListResponse.results != null) {
                    if (orderBeanBaseOilHttpListResponse.results.size() > 0) {
                        for (DIrverOrderBean orderBean : orderBeanBaseOilHttpListResponse.results) {
                            if (orderBean.getOrderStatus() == 6) {
                                continue;
                            }
                            dIrverOrderBean = orderBean;
                            break;
                        }
                        if (dIrverOrderBean == null) {
                            addFragment(TYPE_DIRVER);
                        } else {
                            if (dIrverOrderBean.getOrderStatus() >= 4) {
                                msnLat = dIrverOrderBean.getMeshLat();
                                msLon = dIrverOrderBean.getMeshLng();
                                getMeshData(dIrverOrderBean);
                                llBottom.setVisibility(View.VISIBLE);
                            } else {
                                addFragment(TYPE_DIRVER_ORDER);
                                llBottom.setVisibility(View.GONE);
                            }
                        }

                    } else {
                        addFragment(TYPE_DIRVER);
                    }
                } else {
                    addFragment(TYPE_DIRVER);
                }
            }

            @Override
            public void error(int code, String message) {
                addFragment(TYPE_DIRVER);
            }
        });
    }

    private void getMeshData(DIrverOrderBean dIrverOrderBean) {
        retrofit2.Retrofit retrofit = RetrofitHttp.provideRetrofit();
        DirverApiService dirverApiService = retrofit.create(DirverApiService.class);
        Gson gson = new Gson();
        String strEntity = gson.toJson(dIrverOrderBean);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        dirverApiService.meshState(body).compose(RxSchedulers.compose()).safeSubscribe(new Observer<OilOrderState>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(OilOrderState o) {
                oilOrderState = o;
                addFragment(TYPE_MAP_2);
            }

            @Override
            public void onError(Throwable e) {
                addFragment(TYPE_MAP_2);
            }

            @Override
            public void onComplete() {

            }
        });
    }

    public void myCar() {
        retrofit2.Retrofit retrofit = RetrofitHttp.provideRetrofit();
        OilApiService apiService = retrofit.create(OilApiService.class);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), "{}");
        apiService.myCard(body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpResponse<OilBean>>() {
            @Override
            public void success(BaseOilHttpResponse<OilBean> oilHttpResponse) {
                LogUtils.d("success");
                if (oilHttpResponse!=null&&oilHttpResponse.result!=null){
                    UserService.getInstance().setCarId(oilHttpResponse.result.getCarNum());
                }
                querySwitch();

            }

            @Override
            public void error(int code, String message) {
                addFragment(TYPE_MAP);

            }
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        message();
//        toggleBottom(View.VISIBLE);
    }

    private void addFragment(int type) {
        if (current_type == type) {
            return;
        }
        current_type = type;
        if (type == TYPE_MAP) {
            fragment = new Mapfragment();
            llBottom.setVisibility(View.VISIBLE);
            if (fromtype == 1) {
                Bundle bundle = new Bundle();
                bundle.putInt("type", 1);
                fragment.setArguments(bundle);
            }
        } else if (type == TYPE_OIL) {
            llBottom.setVisibility(View.GONE);
            fragment = new OilHomeFragment();
        } else if (type == TYPE_DIRVER) {
            llBottom.setVisibility(View.GONE);
            fragment = new DirverHomeFragment();
        } else if (type == TYPE_DIRVER_ORDER) {
            llBottom.setVisibility(View.GONE);
            fragment = new DirverHomeFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("type", 1);
            fragment.setArguments(bundle);
        } else if (type == TYPE_MAP_2) {
            fragment = new CommonMapfragment();
            Bundle bundle = new Bundle();
            llBottom.setVisibility(View.VISIBLE);
            bundle.putDouble("latitude", msnLat);
            bundle.putDouble("longitude", msLon);
            bundle.putInt("type", 2);
            if (oilOrderState != null) {
                bundle.putSerializable("oilOrderState", oilOrderState);
            }
            bundle.putSerializable("dIrverOrderBean", dIrverOrderBean);
            fragment.setArguments(bundle);
        }
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); //去掉切换Fragment时闪动的动画效果
        transaction.replace(R.id.home_fm_container, fragment);
        transaction.commitNowAllowingStateLoss();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.iv_home:
                startActivity(new Intent(this, UserCenterActivity.class));
                break;
            case R.id.iv_message:
                if (UserService.getInstance().getRole() == ROLE_DIRVER) {
                    ToastUtils.showShort("此功能即将开放");
                } else {
                    startActivity(new Intent(this, MessageActivity.class));
                }
                break;
            case R.id.iv_message_dirver:
                startActivity(new Intent(this, MessageActivity.class));
                break;
            case R.id.tv_location:
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivityForResult(intent, 887);
                break;
            case R.id.tv1:
                if (UserService.getInstance().getRole() == ROLE_OWNER) {
                    if (fragment != null && fragment instanceof Mapfragment) {
                        ((Mapfragment) fragment).mainJiaYou();
                    }
                } else if (UserService.getInstance().getRole() == ROLE_OIL) {
                    startActivity(new Intent(this, OilDeviceManagerActivity.class));
                } else {
                    startActivity(new Intent(this, DirverOrderActivity.class));
                }
                break;
            case R.id.tv2:
                if (UserService.getInstance().getRole() == ROLE_OWNER) {
                    startActivity(new Intent(this, DeviceManagerActivity.class));
                } else if (UserService.getInstance().getRole() == ROLE_OIL) {
                    startActivity(new Intent(this, OilOwnerOrderActivity.class));
                }
                break;
            case R.id.tv3:
                startActivity(new Intent(this, OrderActivity.class));
                break;
            case R.id.tv4:
                startActivity(new Intent(this, FindDirverActivity.class));

                break;
        }
    }

    private boolean isPause;

    @Override
    protected void onPause() {
        super.onPause();
//        isPause=true;
//        toggleBottom(View.GONE);
    }

    @Override
    protected void onResume() {
        isPause = false;
        if (!GpsUtil.isOPen(getApplicationContext())) {
            tvLocation.setVisibility(View.VISIBLE);
        } else {
            tvLocation.setVisibility(View.GONE);
        }
        super.onResume();
//        llBottom.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                if(!isPause){
//                    toggleBottom(View.VISIBLE);
//                }
//            }
//        },500);
    }


    @Override
    public void OnCheckedChanged(boolean isChecked) {
        if (isChecked) {
            if (UserService.getInstance().getRole() == ROLE_OIL) {
                if (TextUtils.isEmpty(UserService.getInstance().getCarId())) {
                    ToastUtils.showShort("请先添加加油车");
                    switchButton.setChecked(false);
                    return;
                }
                //加油车驾驶员
                addFragment(TYPE_OIL);
                llBottom.setVisibility(View.GONE);
            } else {
                ToastUtils.showShort("此功能即将开放");
                switchButton.setChecked(false);
            }

        } else {
            EventBus.getDefault().post(new OilChangeEvent(false));
            addFragment(TYPE_MAP);
            llBottom.setVisibility(View.VISIBLE);
            if (UserService.getInstance().getRole() == ROLE_OIL) {
                retrofit2.Retrofit retrofit = RetrofitHttp.provideRetrofit();
                //获取接口对象
                OilApiService apiService = retrofit.create(OilApiService.class);
                HandleTick handleTick = new HandleTick();
                handleTick.setLatitude(LocationManager.getInstance().getLatitude());
                handleTick.setLongitude(LocationManager.getInstance().getLongitude());
                handleTick.setRequestTime(System.currentTimeMillis() / 1000);
                RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), new Gson().toJson(handleTick));
                apiService.closeHandle(body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpListResponse<OilHomeBean>>() {

                    @Override
                    public void success(BaseOilHttpListResponse<OilHomeBean> oilOrderBeanBaseOilHttpListResponse) {

                    }

                    @Override
                    public void error(int code, String message) {
                    }
                });
            }
        }
    }

//    public void toggleBottom(int visiable){
//        llBottom.setVisibility(visiable);
//    }

    public void toDirverMap() {
        getList();
    }

    private boolean isLocationStart;

    @Override
    public void onLocationChanged(AMapLocation location) {
        if (null != location) {
            double lat=location.getLongitude();
            if(lat<=0){
                tvLocation.setVisibility(View.VISIBLE);
            }else {
                if(tvLocation.getVisibility()==View.VISIBLE){
                    tvLocation.setVisibility(View.GONE);
                }
            }
            LocationManager.getInstance().pubLishLocation(location);
            if (!isLocationStart) {
                startService(intent);
                isLocationStart = true;
            }
            StringBuffer sb = new StringBuffer();
            if (location.getErrorCode() == 0) {
                tvAddress.setText(location.getCity());
                sb.append("经    度    : " + location.getLongitude() + "\n");
                sb.append("纬    度    : " + location.getLatitude() + "\n");
            }
            String result = sb.toString();
            LogUtils.d(result);
            LogUtils.d("定位成功:" + location.getErrorCode());
        } else {
            tvLocation.setVisibility(View.VISIBLE);
            LogUtils.d("定位失败:" + location.getErrorCode());
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mlocationClient) {
            mlocationClient.onDestroy();
        }
        EventBus.getDefault().unregister(this);
        stopService(intent);
    }

    @Subscribe
    public void onEvent(GiveBackEvent event) {
        addFragment(TYPE_DIRVER);
    }

    boolean isExit;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };

    public void exit() {
        if (!isExit) {
            isExit = true;
            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
            mHandler.sendEmptyMessageDelayed(0, 2000);
        } else {
            ActivityUtils.finishAllActivities();
            System.exit(0);
        }
    }

}
